import {onLoad} from '@dcloudio/uni-app';
import {ref} from 'vue'
import {
    judgeIosPermissionCamera,
    requestAndroidPermission,
    gotoAppPermissionSetting
} from "./lib/permisionCameraAuth.js"

export function useCameraAuth() {
    const isIos = ref(false)

    onLoad(() => {
        // #ifdef APP-PLUS
        isIos.value = (plus.os.name == "iOS")
        // #endif
    })

    // 查询相机权限
    const getCameraAuth = async () => {
        let bol = false
        if (isIos.value) {
            bol = judgeIosPermissionCamera()
        } else {
            const result = await requestAndroidPermission('android.permission.CAMERA')
            if (result == 1) {
                bol = true
            }
        }
        return bol
    }

    // app拒绝授权相机
    const refusedCameraAuth = () => {
        uni.showModal({
            title: "",
            content: "您已禁止授权应用相机权限，如需使用请在设置中授权",
            cancelText: "取消",
            confirmText: "前往授权",
            confirmColor: "#02A8E6",
            showCancel: true,
            success: (res) => {
                if (res.confirm) {
                    openCameraAuth()
                }
            },
        })
    }

    // app去授权
    const openCameraAuth = () => {
        gotoAppPermissionSetting()
    }

    return {
        isIos,
        getCameraAuth,
        refusedCameraAuth,
        openCameraAuth
    }
}